package com.lanou.sinasports.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.sinasports.R;
import com.lanou.sinasports.adapter.NbaAdapter;
import com.lanou.sinasports.beans.GsonNba;
import com.lanou.sinasports.beans.Nba;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/10.
 */
public class NBAFragment extends Fragment {
    private Context context;
    private ListView listView;
    private NbaAdapter adapter;
    private String url = "http://platform.sina.com.cn/sports_client/feed?app_key=2586208540&_version__=3.2.0.14&__os__=android&feed_id=653&f=title%2Curl%2Ccategoryid%2Cimg%2Cimages%2Ccomment_total%2Ccomment_show%2Cctime%2Cvid%2Cvideo_info%2Cstitle%2Cwapsummary&num=20&partner=clear&pos=nba&sport_tour=nba&ad=1&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%221453551156958%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22MI+4LTE%22%2C%22device_type%22%3A%224%22%2C%22osv%22%3A%224.4.4%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%A7%BB%E5%8A%A8%22%2C%22device_id%22%3A%22866500020074540%22%2C%22make%22%3A%22MI+4LTE%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22version%22%3A30200014%2C%22ip%22%3A%22fe80%3A%3A1a59%3A36ff%3Afe27%3A43dd%25wlan0%22%7D%7D&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";
    private VolleySingleton queue;
    private List<Nba> datas = new ArrayList<>();
    String img;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_nba, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listView = (ListView) view.findViewById(R.id.lv_nba);
        listView.addHeaderView(LayoutInflater.from(context).inflate(R.layout.nba_header, null));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        queue = VolleySingleton.getInstance();

        Log.d("NBAFragment", url);
        GsonRequest<GsonNba> request = new GsonRequest<GsonNba>(url, GsonNba.class, new Response.Listener<GsonNba>() {
            @Override
            public void onResponse(GsonNba gsonNba) {
                int size = gsonNba.getResult().getData().getFeed().getData().size();

                for (int i = 0; i < size; i++) {


                    GsonNba.ResultEntity.DataEntity.FeedEntity.DataEntityNb nb = gsonNba.getResult().getData().getFeed().getData().get(i);

                    String stitle = nb.getStitle();
                    String summary = nb.getWapsummary();
                    String img = "";
                    if (nb.getImg() != null) {
                        img = nb.getImg().getU();
                    }
                    String count = "";
                    if (nb.getComment_total() != null) {
                        count = nb.getComment_total() + "评";
                    }
                    String id = nb.getCategoryid();
                    String times = "";
                    String playlink = "";
                    String vid = "";
                    if (id.equals("1")) {
                        Nba nba = new Nba();
                        nba.setCount(count);
                        nba.setStitle(stitle);
                        nba.setSummary(summary);
                        nba.setCategoryid(id);

                        nba.setImg(img);
                        datas.add(nba);
                    } else if (id.equals("3")) {
                        Nba nba = new Nba();
                        times = nb.getVideo_info().getPlay_times() + "播 ";
                        nba.setStitle(stitle);
                        nba.setSummary(summary);
                        nba.setCategoryid(id);
                        nba.setImg(img);
                        nba.setPlay_times(times + "");
                        if (nb.getVideo_info().getPlaylink() != null) {
                            playlink = nb.getVideo_info().getPlaylink();
                            vid = nb.getVideo_info().getVid() + "";
                            nba.setPlaylink(playlink);
                            nba.setVid(vid);


                            datas.add(nba);
                        }
                    }







                }


                adapter = new NbaAdapter(context, datas);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });


        queue._addRequest(request);

    }
}
